package com.xingfly.service;

import com.xingfly.model.BaseModel;
import com.xingfly.model.Follower;
import com.xingfly.model.Following;
import com.xingfly.model.User;
import com.xingfly.repository.FollowerRepository;
import com.xingfly.repository.FollowingRepository;
import com.xingfly.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 关注服务
 * Created by SuperS on 2016/11/29.
 */
@Service
public class FollowService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private FollowerRepository followerRepository;
    @Autowired
    private FollowingRepository followingRepository;

    /**
     * 关注用户
     *
     * @param userId          关注者
     * @param followingUserId 被关注者
     */
    @Transactional(rollbackFor = {IllegalArgumentException.class})
    public boolean followingUser(Long userId, Long followingUserId) {
        Following following = followingRepository.findByUserIdAndFollowingUserId(userId, followingUserId);
        System.out.println(following);
        if (following == null) {
            User user = userRepository.findOne(userId);
            User followingUser = userRepository.findOne(followingUserId);
            if (user != null && followingUser != null) {
                //用户关注操作
                Following saveFollowing = new Following();
                saveFollowing.setUser(user);
                saveFollowing.setFollowingUser(followingUser);
                followingRepository.save(saveFollowing);
                //被关注用户添加粉丝操作
                Follower follower = new Follower();
                follower.setUser(followingUser);
                follower.setFollowerUser(user);
                followerRepository.save(follower);
                return true;
            } else {
                return false;
            }
        } else {
            throw new IllegalArgumentException("关注用户失败!" + following.toString() + "为空才可以进行关注操作!");
        }
    }

    /**
     * 取消关注
     *
     * @param userId          取消关注者
     * @param followingUserId 被取消关注者
     */
    @Transactional(rollbackFor = {IllegalArgumentException.class})
    public boolean unFollowing(Long userId, Long followingUserId) {
        Following following = followingRepository.findByUserIdAndFollowingUserId(userId, followingUserId);
        Follower follower = followerRepository.findByUserIdAndFollowerUserId(followingUserId, userId);
        if (following != null && follower != null) {
            //用户删除关注
            followingRepository.delete(following);
            //被关注者删除粉丝
            followerRepository.delete(follower);
            return true;
        } else {
            throw new IllegalArgumentException("取消关注失败!" + follower.toString() + "|" + following.toString() + "两个关系都不为空时才能取消关注！");
        }
    }

    /**
     * 获取粉丝列表
     *
     * @param userId 用户ID
     */
    public List<User> getFollowers(Long userId) {
        return followerRepository.getFollowersByUserId(userId);
    }

    /**
     * 分页获取粉丝列表
     * */
    public Page<User> getFollowers(Long userId, int page, int size){
        return followerRepository.getFollowersByUserId(userId,new PageRequest(page,size));
    }


    /**
     * 获取粉丝列表ID
     *
     * @param userId 用户ID
     */
    public List<Long> getFollowerIds(Long userId) {
        return followerRepository.getFollowerIdsByUserId(userId);
    }

    /**
     * 获取粉丝数量
     *
     * @param userId 用户ID
     */
    public Long getFollowersNum(Long userId) {
        return followerRepository.getFollowerCountByUserId(userId);
    }

    /**
     * 获取Follower列表
     *
     * @param id 用户ID
     */
    public List<Follower> getFollowersByUserId(Long id) {
        return followerRepository.findByUserId(id);
    }

    /**
     * 获取关注列表
     *
     * @param userId 用户ID
     */
    public List<User> getFollowingsByUserId(Long userId) {
        return followingRepository.findFollowingsByUserId(userId);
    }

    /**
     * 获取关注列表ID
     *
     * @param userId 用户ID
     */
    public List<Long> getFollowingIds(Long userId) {
        return followingRepository.getFollowingIdsByUserId(userId);
    }

    /**
     * 获取关注数量
     *
     * @param userId 用户ID
     */
    public Long getFollowingsNum(Long userId) {
        return followingRepository.getFollowingCountByUserId(userId);
    }

    /**
     * 获取Following列表
     *
     * @param id 用户ID
     */
    public List<Following> getFollowingByUserId(Long id) {
        return followingRepository.findByUserId(id);
    }

    /**
     * 是否关注
     *
     * @param userA 关注者
     * @param userB 被关注者
     */
    public boolean isFollowing(Long userA, Long userB) {
        boolean result = false;
        if (followingRepository.findByUserIdAndFollowingUserId(userA, userB) != null) {
            result = true;
        }
        return result;
    }

    /**
     * 是否关注用户列表中的用户
     *
     * @param userId 用户ID
     * @param users  用户列表
     * @return map key 被关注者ID  value是否关注
     */
    public Map<Long, Boolean> isFollowingUsers(Long userId, List<User> users) {
        if (users == null || users.size() == 0) {
            return null;
        }
        List<Long> users_id = users.stream().map(BaseModel::getId).collect(Collectors.toList());
        List<Following> followings = followingRepository.isFollowingUsers(userId, users_id);
        Map<Long, Boolean> map = new HashMap<>();
        for (Following following : followings) {
            map.put(following.getFollowingUser().getId(), true);
        }
        return map;
    }
}
